package lol.clann.minecraft.springboot.plugin.bukkit.command.parameter;

import lol.clann.minecraft.springboot.plugin.bukkit.command.CommandContext;
import lol.clann.minecraft.springboot.api.bukkit.command.resolver.CommandResolver;
import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;

import java.lang.reflect.Type;

/**
 *
 * @author pyz
 * @date 2019/5/6 10:07 PM
 */
@Getter
@ToString
@Builder
public class NormalParameter {
    private int index;
    private int ignoreIndex;
    @NonNull
    private CommandResolver resolver;
    @Builder.Default
    private boolean required = true;
    private Type type;

    public Object resolve(CommandSender commandSender, CommandContext context, int startIndex) {
        if (index - ignoreIndex + startIndex >= context.getArgs().length) {
            if (required) {
                throw new CommandException(String.format("缺少第" + (index + 1) + "个参数!"));
            }
            return null;
        }
        return resolver.resolve(commandSender, context.getArgs()[index - ignoreIndex + startIndex], type);
    }
}
